﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TiTi.tea.Comm;
using TiTi.tea.DAL;
using TiTi.tea.Dto;
using TiTi.tea.Model;

namespace TiTi.tea.BLL
{
    public class OrderInfoBLL
    {
        private OrderInfoDAL orderDal = new OrderInfoDAL();

        /// <summary>
        /// 订单添加
        /// </summary>
        /// <param name="info"></param>
        /// <param name="details"></param>
        /// <returns></returns>
        public OrderDto Add(orderinfo info, List<OrderDetail> details)
        {
            OrderDetailDAL detailDal = new OrderDetailDAL();
            SkuDAL skuDal = new SkuDAL();

            info.OrderId = Tools.GetSnowFlakeId();
            info.CreateTime = DateTime.Now;
            info.UpdateTime = DateTime.Now;
            info.EndTime = DateTime.Now.AddMinutes(20);
            info.IsDelete = 0;
            info.Status = 1;

            orderDal.Add(info, false);

            foreach (var item in details)
            {

                detailDal.Add(new OrderDetail()
                {
                    OrderId = info.OrderId,
                    GoodName = item.GoodName,
                    SkuId = item.SkuId,
                    Price = item.Price,
                    Num = item.Num,
                    Image = item.Image,
                    MaterialItem = item.MaterialItem,
                    SpecItem = item.SpecItem
                }, false);
            }
            if (!detailDal.SaveChanges()) return null;
            return new OrderDto()
            {
                id = info.OrderId,
                amount = info.Amount ?? 0,
                paymenTime = info.PaymentTime,
                createTime = info.CreateTime,
                updateTime = info.UpdateTime,
                endTime = info.EndTime,
                status = info.Status ?? 0,
                remark = info.Remark,
            };
        }

        /// <summary>
        /// 获取用户今日订单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public IEnumerable<OrderDto> GetOrderinfos(int userId)
        {
            OrderDetailDAL detailDal = new OrderDetailDAL();
            var infos = orderDal.GetAll(o => o.UserId == userId)
            .Where(o => (o.Status < 4 && o.OrderType == 1) || (o.Status < 5 && o.OrderType == 2));
            var details = detailDal.GetAll();

            return from info in infos
                   select new OrderDto()
                   {
                       id = info.OrderId,
                       amount = info.Amount ?? 0,
                       paymenTime = info.PaymentTime,
                       createTime = info.CreateTime,
                       updateTime = info.UpdateTime,
                       endTime = info.EndTime,
                       status = info.Status ?? 0,
                       remark = info.Remark,
                       type = info.OrderType ?? 0,
                       takeCode = info.TakeCode,
                       details = details.Where(d => d.OrderId == info.OrderId).Select(d => new OrderDetailDto()
                       {
                           name = d.GoodName,
                           price = d.Price ?? 0,
                           num = d.Num ?? 0,
                           image = d.Image
                       })
                   };
        }

        /// <summary>
        /// 获取用户历史订单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public IEnumerable<OrderDto> GetCustomerOrders(int userId)
        {
            var data = Get();
            return data.Where(o => o.status == 0 || (o.type == 1 && o.status == 3) || (o.type == 2 && o.status == 4));
        }

        /// <summary>
        /// 支付订单
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public bool OrderBuy(long orderId)
        {
            var order = Get(orderId);
            order.Status = 2;
            order.PaymentTime = DateTime.Now;
            // 自取添加取餐号
            if (order.OrderType == 1) order.TakeCode = GetTakeCode();
            return orderDal.SaveChanges();
        }

        /// <summary>
        /// 生成取餐号
        /// </summary>
        /// <returns></returns>
        public int GetTakeCode()
        {
            Random rand = new Random();
            var data = orderDal.GetAll(o => o.Status == 2);
            int code;
            while (true)
            {
                code = rand.Next(1000, 10000);
                if (data.FirstOrDefault(o => o.TakeCode == code) == null) break;
            }
            return code;
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <returns></returns>
        public bool UpdateStatus(long id, int status)
        {
            var order = Get(id);
            order.Status = status;
            return orderDal.SaveChanges();
        }

        /// <summary>
        /// 根据订单id获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public orderinfo Get(long id)
        {
            return orderDal.GetOne(o => o.OrderId == id);
        }

        /// <summary>
        /// 获取订单详细
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public IEnumerable<OrderDetail> GetDetails(long orderId)
        {
            OrderDetailDAL detailDal = new OrderDetailDAL();
            return detailDal.GetAll(o => o.OrderId == orderId);
        }

        /// <summary>
        /// 根据id获取订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OrderDto GetOrderInfoDetails(long id)
        {
            var info = Get(id);
            var detail = GetDetails(id);
            return new OrderDto()
            {
                id = info.OrderId,
                amount = info.Amount ?? 0,
                paymenTime = info.PaymentTime,
                createTime = info.CreateTime,
                updateTime = info.UpdateTime,
                endTime = info.EndTime,
                status = info.Status ?? 0,
                remark = info.Remark,
                type = info.OrderType ?? 0,
                takeCode = info.TakeCode,
                details = detail.Select(d => new OrderDetailDto()
                {
                    name = d.GoodName,
                    price = d.Price ?? 0,
                    num = d.Num ?? 0,
                    image = d.Image,
                    MaterialItem = d.MaterialItem,
                    SpecItem = d.SpecItem
                })
            };
        }

        /// <summary>
        /// 根据状态获取订单
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public IEnumerable<OrderDto> Get(int status)
        {
            OrderDetailDAL detailDal = new OrderDetailDAL();

            var infos = orderDal.GetAll(o => o.Status == status);
            var details = detailDal.GetAll();

            return from info in infos
                   select new OrderDto()
                   {
                       id = info.OrderId,
                       amount = info.Amount ?? 0,
                       paymenTime = info.PaymentTime,
                       createTime = info.CreateTime,
                       updateTime = info.UpdateTime,
                       endTime = info.EndTime,
                       status = info.Status ?? 0,
                       remark = info.Remark,
                       type = info.OrderType ?? 0,
                       takeCode = info.TakeCode,
                       details = details.Where(d => d.OrderId == info.OrderId).Select(d => new OrderDetailDto()
                       {
                           name = d.GoodName,
                           price = d.Price ?? 0,
                           num = d.Num ?? 0,
                           image = d.Image,
                           SpecItem = d.SpecItem,
                           MaterialItem = d.MaterialItem
                       })
                   };
        }

        /// <summary>
        /// 获取全部订单
        /// </summary>
        /// <returns></returns>
        public IEnumerable<OrderDto> Get()
        {
            OrderDetailDAL detailDal = new OrderDetailDAL();

            var infos = orderDal.GetAll();
            var details = detailDal.GetAll();

            return from info in infos
                   select new OrderDto()
                   {
                       id = info.OrderId,
                       amount = info.Amount ?? 0,
                       paymenTime = info.PaymentTime,
                       createTime = info.CreateTime,
                       updateTime = info.UpdateTime,
                       endTime = info.EndTime,
                       status = info.Status ?? 0,
                       remark = info.Remark,
                       type = info.OrderType ?? 0,
                       takeCode = info.TakeCode,
                       details = details.Where(d => d.OrderId == info.OrderId).Select(d => new OrderDetailDto()
                       {
                           name = d.GoodName,
                           price = d.Price ?? 0,
                           num = d.Num ?? 0,
                           image = d.Image,
                           SpecItem = d.SpecItem,
                           MaterialItem = d.MaterialItem
                       })
                   };
        }
    }
}
